<template>
  <div class="login_container">
    <div class="login_box">
      <!--      头像-->
      <div class="avatar_box">
        <img src="../assets/logo.png" alt="">
      </div>
      <!--      表单-->
      <el-form ref="loginfromref" class="login_form" v-bind:model="formInfo" :rules="fromrules">
        <el-form-item prop="name">
          <el-input v-model="formInfo.name" placeholder="请输入用户名" prefix-icon="iconfont icon-user"/>
        </el-form-item>
        <el-form-item prop="password">
          <el-input v-model="formInfo.password" type="password" placeholder="请输入密码"
                    prefix-icon="iconfont icon-3702mima"/>
        </el-form-item>
        <el-form-item class="login_btns">
          <el-button type="primary" v-on:click="onsubmit(formInfo)">提交</el-button>
          <el-button type="info" v-on:click="onResetClick">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data () {
    return {
      formInfo: {
        name: 'admin',
        password: '123456'
      },
      fromrules: {
        // name字段名需要跟info里面的name字段名一致
        name: [
          {
            required: true,
            message: '请输入用户名',
            trigger: 'blur'
          },
          {
            min: 3,
            max: 5,
            message: '长度在 3 到 5 个字符',
            trigger: 'blur'
          }
        ],
        password: [
          {
            required: true,
            message: '请输入密码',
            trigger: 'blur'
          },
          {
            min: 3,
            max: 6,
            message: '密码长度为3到6个字符',
            trigger: 'blur'
          }
        ]
      }
    }
  },
  methods: {
    onsubmit: function (info) {
      this.$refs.loginfromref.validate(async (valid) => {
        if (!valid) {
          this.$message.error('校验失败')
        } else {
          // 将返回对象中的data字段重命名为res
          const { data: res } = await this.$http.post('login', {
            username: this.formInfo.name,
            password: this.formInfo.password
          })
          console.log(res)
          if (res.meta.status === 200) {
            this.$message.success('登录成功')
            window.sessionStorage.setItem('token', res.data.token)
            this.$router.push('/home')
          } else {
            this.$message.error('登录失败')
          }
        }
      })
    },
    onResetClick: function () {
      this.$refs.loginfromref.resetFields()
    }
  }
}
</script>

<style scoped lang="less">
.login_container {
  width: 100%;
  height: 100%;
  background-color: #2b4b6b;
}

.login_box {
  width: 450px;
  height: 300px;
  background-color: #fff;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
}

.avatar_box {
  width: 130px;
  height: 130px;
  background-color: #fff;
  border: 1px solid #eee;
  border-radius: 50%;
  box-sizing: border-box;
  padding: 10px;
  box-shadow: 0 0 10px #eee;
  position: absolute;
  left: 50%;
  transform: translate(-50%, -50%);

  img {
    width: 100%;
    height: 100%;
    border-radius: 50%;
    background-color: #eee;
  }
}

.login_form {
  width: 100%;
  position: absolute;
  bottom: 0;
  box-sizing: border-box;
  padding: 0 10px;

  .login_btns {
    display: flex;
    flex-direction: row;
    justify-content: flex-end;
  }
}

//.avatar_box img {
//  width: 100%;
//  height: 100%;
//  border-radius: 50%;
//  background-color: #eee;
//}
</style>
